using OpenBlock.Core;
using OpenBlock.Item;
using OpenBlock.Utils;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace OpenBlock.GUI.Panels
{
    public class BrushPalettePanel : BasePanel
    {
        [SerializeField]
        private Image preview;
        [SerializeField]
        private Slider r, g, b;

        private Color color = Color.black;
        public ItemState brush;

        public override void OnResume()
        {
            base.OnResume();
            GameManager.Instance.SetGameStage(GameManager.GameStage.Pause);
            
            if(userData != null)
            {
                brush = (ItemState)userData;
                color = brush.GetProperty("RGB").GetUint().ToColor();
            }
            UpdatePreview();
        }

        public void OnSetRed(float value)
        {
            color.r = value;
            UpdatePreview();
        }

        public void OnSetGreen(float value)
        {
            color.g = value;
            UpdatePreview();
        }

        public void OnSetBlue(float value)
        {
            color.b = value;
            UpdatePreview();
        }

        public void OnConfirm()
        {
            if (brush != null) brush.SetProperty("RGB", color.ToColorCode());
            GameManager.Instance.SetGameStage(GameManager.GameStage.Game);
            manager.CloseCurrentPanel();
        }

        public void UpdatePreview()
        {
            preview.color = color;
            r.value = color.r;
            g.value = color.g;
            b.value = color.b;
        }
    }
}